<template>
    <div class="app-container">
        <el-row :gutter="20">
            <!--科目数据-->
            <el-col :span="4" :xs="24">
                <div class="head-container">
                    <el-input
                        v-model="querySubjectParams.pName"
                        placeholder="请输入学科名称"
                        clearable
                        size="small"
                        prefix-icon="el-icon-search"
                        style="margin-bottom: 20px"
                    />
                </div>
                <div class="head-container subject-container">
                    <el-table v-loading="loading" :data="subjectList" @row-click="subjectListdbclick"  highlight-current-row ref="monthlyPlanTable">
                        <el-table-column label="学科" align="center" prop="pName" show-overflow-tooltip/>
                    </el-table>
                </div>
            </el-col>
        <el-col :span="20" :xs="24">
            <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
                <el-form-item label="事件标题" prop="title">
                    <el-input
                    v-model="queryParams.title"
                    placeholder="请输入事件标题"
                    clearable
                    @keyup.enter.native="handleQuery"
                    />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                    <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
                </el-form-item>
            </el-form>

            <el-row :gutter="10" class="mb8">
                <el-col :span="1.5">
                    <el-button
                    type="primary"
                    plain
                    icon="el-icon-plus"
                    size="mini"
                    @click="handleAdd"
                    :disabled="addbtn"
                    v-hasPermi="['plan:content:add']"
                    >新增</el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button
                    type="success"
                    plain
                    icon="el-icon-edit"
                    size="mini"
                    :disabled="single"
                    @click="handleUpdate"
                    v-hasPermi="['plan:content:edit']"
                    >修改</el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button
                    type="danger"
                    plain
                    icon="el-icon-delete"
                    size="mini"
                    :disabled="multiple"
                    @click="handleDelete"
                    v-hasPermi="['plan:content:remove']"
                    >删除</el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button
                    type="warning"
                    plain
                    icon="el-icon-download"
                    size="mini"
                    @click="handleExport"
                    v-hasPermi="['plan:content:export']"
                    >导出</el-button>
                </el-col>
                    <right-toolbar :showSearch.sync="showSearch" @queryTable="getSubjectList"></right-toolbar>
                </el-row>

                <el-table v-loading="loading" :data="contentList" @selection-change="handleSelectionChange">
                    <el-table-column type="selection" width="55" align="center" />
                    <!-- <el-table-column label="主键，自增" align="center" prop="id" />
                    <el-table-column label="科目外键" align="center" prop="subjectId" /> -->
                    <el-table-column label="事件标题" align="center" prop="title" show-overflow-tooltip/>
                    <el-table-column label="事件内容" align="center" prop="content" show-overflow-tooltip/>
                    <el-table-column label="学习状态" align="center" prop="studyStatus">
                        <template slot-scope="scope">
                        <dict-tag :options="dict.type.is_study_wether" :value="scope.row.studyStatus"/>
                        </template>
                    </el-table-column>
                    <el-table-column label="今日状态" align="center" prop="markStatus">
                        <template slot-scope="scope">
                        <dict-tag :options="dict.type.is_study_wether" :value="scope.row.markStatus"/>
                        </template>
                    </el-table-column>
                    <el-table-column label="开启排队" align="center" prop="isQueue">
                        <template slot-scope="scope">
                        <dict-tag :options="dict.type.is_quene" :value="scope.row.isQueue"/>
                        </template>
                    </el-table-column>
                    <el-table-column label="创建日期" align="center" prop="createDate" width="180">
                        <template slot-scope="scope">
                        <span>{{ parseTime(scope.row.createDate, '{y}-{m}-{d}') }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column label="备注" align="center" prop="describes" show-overflow-tooltip/>
                    <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                        <template slot-scope="scope">
                        <el-button
                            size="mini"
                            type="text"
                            icon="el-icon-edit"
                            @click="handleUpdate(scope.row)"
                            v-hasPermi="['plan:content:edit']"
                        >修改</el-button>
                        <el-button
                            size="mini"
                            type="text"
                            icon="el-icon-delete"
                            @click="handleDelete(scope.row)"
                            v-hasPermi="['plan:content:remove']"
                        >删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            
                <pagination
                    v-show="total>0"
                    :total="total"
                    :page.sync="queryParams.pageNum"
                    :limit.sync="queryParams.pageSize"
                    @pagination="getList"
                />
            </el-col>
        </el-row>
  
        <!-- 添加或修改学习内容对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
            <!-- <el-form-item label="科目外键" prop="subjectId">
            <el-input v-model="form.subjectId" placeholder="请输入科目外键" />
            </el-form-item> -->
            <el-form-item label="事件标题" prop="title">
            <el-input v-model="form.title" placeholder="请输入事件标题" />
            </el-form-item>
            <el-form-item label="事件内容" prop="content">
                <editor v-model="form.content" :min-height="192"/>
            </el-form-item>
            <!-- <el-form-item label="学习状态" prop="studyStatus">
            <el-radio-group v-model="form.studyStatus">
                <el-radio
                v-for="dict in dict.type.is_study_wether"
                :key="dict.value"
                :label="parseInt(dict.value)"
                >{{dict.label}}</el-radio>
            </el-radio-group>
            </el-form-item> -->
            <!-- <el-form-item label="今日状态" prop="markStatus">
            <el-radio-group v-model="form.markStatus">
                <el-radio
                v-for="dict in dict.type.is_study_wether"
                :key="dict.value"
                :label="parseInt(dict.value)"
                >{{dict.label}}</el-radio>
            </el-radio-group>
            </el-form-item> -->
            <el-form-item label="开启排队" prop="isQueue">
            <el-radio-group v-model="form.isQueue">
                <el-radio
                v-for="dict in dict.type.is_quene"
                :key="dict.value"
                :label="parseInt(dict.value)"
                >{{dict.label}}</el-radio>
            </el-radio-group>
            </el-form-item>
            <!-- <el-form-item label="创建日期" prop="createDate">
            <el-date-picker clearable
                v-model="form.createDate"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择创建日期">
            </el-date-picker>
            </el-form-item> -->
            <el-form-item label="备注" prop="describes">
            <el-input v-model="form.describes" type="textarea" placeholder="请输入内容" />
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
        </div>
        </el-dialog>
    </div>
</template>
  
<script>
import { listSubject} from "@/api/plan/subject";
import { listContent, getContent, delContent, addContent, updateContent } from "@/api/plan/content";

export default {
    name: "Content",
    dicts: ['is_quene', 'is_study_wether'],
    data() {
      return {
        subjectList: [],
        // 查询参数
        querySubjectParams: {
            pageNum: 1,
            pageSize: 10,
            pName: null,
        },
        // 部门树选项
        subjectTitleOptions: undefined,
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 新增按钮禁用
        addbtn: true,
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 学习内容表格数据
        contentList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          title: null,
          subjectId:undefined
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          subjectId: [
            { required: true, message: "学科表的外键不能为空", trigger: "blur" }
          ],
          title: [
            { required: true, message: "事件标题不能为空", trigger: "blur" }
          ],
          content: [
            { required: true, message: "事件内容不能为空", trigger: "blur" }
          ],
          studyStatus: [
            { required: true, message: "学习的状态不能为空", trigger: "change" }
          ],
          markStatus: [
            { required: true, message: "今日内容展示状态不能为空", trigger: "change" }
          ],
          isQueue: [
            { required: true, message: "任务开启排队不能为空", trigger: "change" }
          ],
          createDate: [
            { required: true, message: "事件创建日期不能为空", trigger: "blur" }
          ],
        }
      };
    },
    watch: {
        // 根据名称筛选部门树
        "querySubjectParams.pName"(val) {
            // this.$refs.tree.filter(val);
            // console.log(val);
            this.getSubjectList();
        },
        subjectList: function() {
            this.$nextTick(function() {
                this.$refs.monthlyPlanTable.setCurrentRow(this.subjectList[0])
            })
        }

    },
    created() {
      this.getSubjectList();
    },
    methods: {
        // 筛选节点
        subjectListdbclick(row) {
            this.queryParams.subjectId = row.id;
            this.getList();
        },
        /** 查询计划科目列表 */
        getSubjectList() {
            this.loading = true;
            listSubject(this.querySubjectParams).then(response => {
                this.subjectList = response.rows;
                this.loading = false;
                //内容联动  查询后默认显示第一个的内容
                if(this.subjectList.length>0){
                    this.addbtn = false;
                    this.queryParams.subjectId = this.subjectList[0].id;
                    this.getList();
                }else{
                    this.addbtn = true;
                    this.queryParams.subjectId = 909090;
                    this.contentList = [];
                    this.total = 0;
                }
            });
        },
        /** 查询学习内容列表 */
        getList() {
            this.loading = true;
            listContent(this.queryParams).then(response => {
                this.contentList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
            id: null,
            // subjectId: null,
            title: null,
            content: null,
            studyStatus: null,
            markStatus: null,
            isQueue: null,
            createDate: null,
            describes: null
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.id)
            this.single = selection.length!==1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = "添加学习内容";
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const id = row.id || this.ids
            getContent(id).then(response => {
            this.form = response.data;
            this.open = true;
            this.title = "修改学习内容";
            });
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate(valid => {
            if (valid) {
                if (this.form.id != null) {
                updateContent(this.form).then(response => {
                    this.$modal.msgSuccess("修改成功");
                    this.open = false;
                    this.getList();
                });
                } else {
                this.form.subjectId = this.queryParams.subjectId;
                addContent(this.form).then(response => {
                    this.$modal.msgSuccess("新增成功");
                    this.open = false;
                    this.getList();
                });
                }
            }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$modal.confirm('是否确认删除学习内容编号为"' + ids + '"的数据项？').then(function() {
            return delContent(ids);
            }).then(() => {
            this.getList();
            this.$modal.msgSuccess("删除成功");
            }).catch(() => {});
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download('plan/content/export', {
            ...this.queryParams
            }, `content_${new Date().getTime()}.xlsx`)
        }
    }
};
</script>

<style lang="scss" scoped>
.subject-container{
        /* 用来设置当前页面element全局table 选中某行时的背景色*/
        ::v-deep.el-table__body tr.current-row > td.el-table__cell{
            background-color: #304156 !important;
            color: #fff;
        }
    }

</style>
  